//
//  TestTableViewCell.swift
//  SunxianhuaTestApp
//
//  Created by abc on 2021/10/11.
//

import Foundation
import SnapKit
import SKPhotoBrowser
class NewsTableViewCell: UITableViewCell {
    
    /// 定义回调
    typealias CallbackValue=()->Void
    /// 声明闭包
    var chooesCallbackValue:CallbackValue?

    
    var titleLabel :UILabel!
    
    private var lableView :UIView!
    private var nameLabel :UILabel!
    private var dateLabel :UILabel!
    private var iconImageView :UIImageView!
    private var contentLabel :UILabel!
    private var line :UIView = UIView.init()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.creatView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func creatView(){
        
        self.selectionStyle = .none
        self.titleLabel = UILabel.init().then({
            self.contentView.addSubview($0)
            $0.text = "标题"
            $0.snp.makeConstraints({ (make) in
                make.left.equalTo(15.0)
                make.right.equalTo(-15.0)
                make.top.equalTo(15.0)
                make.height.equalTo(40.0)
            })
            $0.numberOfLines = 0
            $0.font = UIFont.boldSystemFont(ofSize: 20.0)
            $0.textColor = UIColor.colorUIHex("#0B1833")
        })
        
        self.lableView = UIView.init().then({
            self.addSubview($0)
            $0.snp.makeConstraints({ (make) in
                make.left.equalTo(15)
                make.right.equalTo(-15)
                make.height.equalTo(50.0)
                make.top.equalTo(titleLabel.snp.bottom).offset(0.0)
            })
        })
        
        self.nameLabel = UILabel.init().then({
            lableView.addSubview($0)
            $0.text = "**"
            $0.snp.makeConstraints({ (make) in
                make.centerY.equalTo(lableView)
                make.left.equalTo(0.0)
                make.height.equalTo(40.0)
            })
            
            $0.textColor = UIColor.colorUIHex("#0B1833")
            $0.font = UIFont.systemFont(ofSize: 12.0)
           
        })
        
        let theLine = UIView.init().then({
            lableView.addSubview($0)
            $0.snp.makeConstraints { make in
                make.left.equalTo(nameLabel.snp.right).offset(5.0)
                make.width.equalTo(1.0)
                make.height.equalTo(12)
                make.centerY.equalToSuperview()
            }
            $0.backgroundColor = .lightGray
        })
        
        
        self.dateLabel = UILabel.init().then({
            lableView.addSubview($0)
            $0.text = "2020/04/20"
            $0.snp.makeConstraints({ (make) in
                make.centerY.equalToSuperview()
                make.left.equalTo(theLine.snp.right).offset(5.0)
            })
            $0.textColor = UIColor.colorUIHex("#848B98")
            $0.font = UIFont.systemFont(ofSize: 12.0)
            $0.textAlignment = .left
        })
        
        
        self.iconImageView = UIImageView.init().then({
            self.contentView.addSubview($0)
            $0.snp.makeConstraints { make in
                make.top.equalTo(lableView.snp.bottom)
                make.left.right.equalTo(self.titleLabel)
                make.height.equalTo(250.0)
            }
            $0.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickBrowerImage))
            $0.addGestureRecognizer(tap)
            $0.contentMode = .scaleAspectFit
            $0.backgroundColor = UIColor.set_Color(200, green: 200, blue: 200, alpha: 0.6)
        })
        
        
        self.contentLabel = UILabel.init().then({
            
            self.contentView.addSubview($0)
            $0.textColor = UIColor.colorUIHex("#848B98")
            $0.font = UIFont.systemFont(ofSize: 16.0)
            $0.snp.makeConstraints({ (make) in
                make.left.right.equalTo(titleLabel)
                make.top.equalTo(iconImageView.snp.bottom).offset(0.0)
                make.height.equalTo(80.0)
            })
            $0.text = ""
            $0.numberOfLines = 0
        })
    
        self.isUserInteractionEnabled = true
        
        self.line = UIView.init().then({
            self.addSubview($0)
            $0.backgroundColor = UIColor.colorUIHex("#F5F5F5")
            $0.snp.makeConstraints({ (make) in
                make.left.equalTo(5.0)
                make.right.equalTo(-15.0)
                make.bottom.equalToSuperview().offset(0.0)
                make.height.equalTo(1.0)
                
                make.bottom.equalToSuperview()
            })
        })
        
    }
    
    @objc func clickBrowerImage(){
        self.chooesCallbackValue?()
    }

    
    
    func reloadCellWith(cellModel :ArticleItemModel,browerImageBlock :CallbackValue?){
        
        self.titleLabel.text = cellModel.title
        self.nameLabel.text = cellModel.author
        self.dateLabel.text = cellModel.publishedAt
        self.contentLabel.text = cellModel.content
        self.iconImageView.loadImageURL(imageUrl: cellModel.urlToImage, placeholderImageString: "photo_ico_default")
        self.chooesCallbackValue = browerImageBlock
        
        //改变布局
        self.titleLabel.snp.updateConstraints { make in
            make.height.equalTo(cellModel.titleHeight)
        }
        self.contentLabel.snp.updateConstraints { make in
            make.height.equalTo(cellModel.contentHeight)
        }
        
        if cellModel.urlToImage.length == 0 {
            self.iconImageView.isHidden = true
            self.contentLabel.snp.updateConstraints { make in
                make.top.equalTo(lableView.snp.bottom).offset(0.0)
            }
        }else{
            self.iconImageView.isHidden = false
            self.contentLabel.snp.updateConstraints { make in
                make.top.equalTo(iconImageView.snp.bottom).offset(0.0)
            }
        }
        
    }
    
    
}
